package shootingtactics.client.gamebase;

import java.util.ArrayList;

import shootingtactics.client.core.Constant;
import shootingtactics.client.core.enemy.BaseEnemy;
import shootingtactics.client.core.enemy.LoopEnemy;

import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;
import com.google.gwt.user.client.Timer;

public class GameBase{

    Context2d backgroundContext;
    Context2d bulletContext;
    Context2d enemyContext;

    Timer timer;

    ArrayList<BaseEnemy> enemyList = new ArrayList<BaseEnemy>();

    final CssColor redrawColor = CssColor.make("rgba(0, 0, 0, 1)");
    final CssColor textColor = CssColor.make("rgba(255, 255, 255, 1)");

    public GameBase(Context2d backgroundContext, Context2d bulletContext, Context2d enemyContext){
        this.backgroundContext = backgroundContext;
        this.bulletContext = bulletContext;
        this.enemyContext = enemyContext;
        initBase();
        setBackground();
    }

    private void setBackground(){
        backgroundContext.setFillStyle(redrawColor);
        backgroundContext.fillRect(0, 0, Constant.CANVAS_WIDTH, Constant.CANVAS_HEIGHT);
    }

    private void initBase(){
        CssColor enemy01Color = CssColor.make("rgba(0,255,255,1)");
        CssColor enemy02Color = CssColor.make("rgba(255,255,0,1)");

        LoopEnemy enemy01 = new LoopEnemy(100, 100, Constant.DRAW_TYPE_RECTANGLE, enemy01Color, enemyContext, bulletContext);
        enemy01.position.setSize(30, 40);

        LoopEnemy enemy02 = new LoopEnemy(700, 500, Constant.DRAW_TYPE_RECTANGLE, enemy02Color, enemyContext, bulletContext);
        enemy02.position.setSize(30, 40);

        enemy01.setTargetEnemy(enemy02);
        enemy02.setTargetEnemy(enemy01);

        enemyList.add(enemy01);
        enemyList.add(enemy02);
    }

    public void stopTimer(){
        if(timer != null){
            timer.cancel();
        }
    }

    public void startTimer(){
        timer = new Timer(){
            @Override
            public void run(){
                doUpdate();
            }
        };
        timer.scheduleRepeating(32);
    }

    public void resetTimer(){
        if(timer != null){
            timer.cancel();
            clearBase();
        }
    }

    private void clearBase(){
        enemyList.clear();
        initBase();
    }

    protected void doUpdate(){

        //long time = System.currentTimeMillis();
        //enemyContext.fillRect(0, 0, Constant.CANVAS_WIDTH, Constant.CANVAS_HEIGHT);
        bulletContext.setFillStyle(redrawColor);
        bulletContext.fillRect(0, 0, Constant.CANVAS_WIDTH, Constant.CANVAS_HEIGHT);
        
        enemyContext.beginPath();
        bulletContext.beginPath();
        for(BaseEnemy enemy:enemyList){
            enemy.updateSpeed();
            enemy.setDraw();
        }
        setHealth();
        enemyContext.closePath();
        bulletContext.closePath();
        //System.out.println("AllDrawTime:" + (System.currentTimeMillis() - time));
    }

    private void setHealth(){
        enemyContext.setFillStyle(textColor);
        enemyContext.fillText(String.valueOf(enemyList.get(0).health), 50, 30, 40);
        enemyContext.fillText(String.valueOf(enemyList.get(1).health), 750, 30, 40);
    }
}
